﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonLib;
using System.Windows.Forms;
using System.IO;

namespace Editor
{
    public class PluginManager
    {
        public List<IPluginShading> Plugins { get; set; }
        public static PluginManager Current { get; set; }
        public PluginManager()
        {
            Plugins = new List<IPluginShading>();
        }
        public void LoadPlugins()
        {
            string sPath = Application.StartupPath;
            string pluginPath = Path.Combine(sPath, "Plugins");
            foreach (string file in Directory.GetFiles(pluginPath, "*.dll"))
            {
                System.Reflection.Assembly a = System.Reflection.Assembly.LoadFile(file);
                foreach (Type t in a.GetTypes())
                {
                    if (t.GetInterfaces().Any(i => i.Equals(typeof(IPluginShading))))
                    {
                        var p = (IPluginShading)Activator.CreateInstance(t);
                        Plugins.Add(p);
                    }
                }
            }
        }
        public IPluginShading GetPluginByID(string id)
        {
            return Plugins.FirstOrDefault(p => id == p.ID);
        }

        internal IPluginShading GetAnyPlugin()
        {
            if (Plugins.Count > 0)
                return Plugins[0];
            return null;
        }
    }
}
